import Vue from 'vue'
import Router from 'vue-router'

/* Layout */
// import Layout from '@/layout'

Vue.use(Router)

// 重写路由的 push 和 replace 方法, 防止点击面包屑报错
const originalPush = Router.prototype.push
const originalReplace = Router.prototype.replace
// push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch((err) => err)
}
// replace
Router.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalReplace.call(this, location, onResolve, onReject)
  }
  return originalReplace.call(this, location).catch((err) => err)
}
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
const isHideDemo = false
const exampleRoutes = []

const projConstRoutes = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/pages/home/index'),
    hidden: isHideDemo,
  },

  {
    path: '/about',
    name: 'about',
    component: () => import('@/pages/about/index'),
    hidden: isHideDemo,
  },

  // {
  //   // hidden: isHideDemo,
  //   path: '/',
  //   component: Layout,
  //   redirect: '/dashboard',
  //   children: [
  //     {
  //       path: 'dashboard',
  //       name: 'Dashboard',
  //       component: () => import('@/pages/dashboard/index'),
  //       meta: { title: '欢迎页', icon: 'dashboard' },
  //     },
  //   ],
  // },

  // {
  //   hidden: isHideDemo,
  //   // path: 'external-link',
  //   path: '',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
  //       meta: { title: 'External Link', icon: 'link' },
  //     },
  //   ],
  // },
]
export const constantRoutes = exampleRoutes.concat(projConstRoutes)

export const asyncRoutes = [
  {
    path: '/404',
    // component: Layout,
    // hidden: true,
    component: () => import('@/pages/404'),
    // redirect: '/404/404',
    // children: [
    //   {
    //     path: '404',
    //     name: '404',
    //     component: () => import('@/pages/404'),
    //     meta: { title: '404', icon: 'form' },
    //   },
    // ],
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true },
]

// hash 模式 和 history 模式的区别
// 当我们在用vue-router 中的hash模式，最后打包生成的文件，不需要后台做什么更改。直接可以用、
// 但是History模式，后台部署时类似于nginx这种，就要加些代码、
// e.g
// location / {
//   root  /Users/jhj/Desktop/mall;
//   index index.html index.htm;
//   try_files $uri $uri/ /index.html;
// }
// 这段代码的意思就是告诉服务器，前边请求啥你都去找index.html, 其他你都不要干涉、
// 不加这段的话，当代码部署的时候就会发现你的路由失效了。不能刷新等等情况。

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  })

const router = createRouter()
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
